<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('序号')" prop="num">
            <el-input-number v-model="form.num" :placeholder="$t('序号')" :controls="false" :precision="0" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('违约债券名称')" prop="name">
            <el-input v-model="form.name" :placeholder="$t('违约债券名称')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('违约债券编码')" prop="number">
            <el-input v-model="form.number" :placeholder="$t('违约债券编码')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('违约债券市值(元)')" prop="marketValue">
            <Money v-model="form.marketValue" :placeholder="$t('违约债券市值(元)')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('违约债券估值依据')" prop="valuationBasis">
            <dict-select
              v-model="form.valuationBasis"
              dictionary="valuation_basis"
              :placeholder="$t('违约债券估值依据')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('违约债券成本(元)')" prop="cost">
            <Money v-model="form.cost" :placeholder="$t('违约债券成本(元)')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('违约债券负面情况类别')" prop="negative">
            <dict-select
              v-model="form.negative"
              dictionary="negative"
              :placeholder="$t('违约债券负面情况类别')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('违约债券发行日期')" prop="issuedDate">
            <el-date-picker
              v-model="form.issuedDate"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('违约债券发行日期')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('违约债券到期时间')" prop="expireDate">
            <el-date-picker
              v-model="form.expireDate"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('违约债券到期时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('债券首次发生违约时间')" prop="defaultDate">
            <el-date-picker
              v-model="form.defaultDate"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('债券首次发生违约时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('违约债券面值(元)')" prop="faceValue">
            <Money v-model="form.faceValue" :placeholder="$t('违约债券面值(元)')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('违约债券延期偿还的本金(元)')" prop="principal">
            <Money v-model="form.principal" :placeholder="$t('违约债券延期偿还的本金(元)')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('违约债券延期偿还的利息(元)')" prop="interest">
            <Money v-model="form.interest" :placeholder="$t('违约债券延期偿还的利息(元)')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('违约债券类别')" prop="type">
            <dict-select
              v-model="form.type"
              dictionary="default_bonds_type"
              :placeholder="$t('违约债券类别')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('违约前债项评级')" prop="debtRating">
            <el-input v-model="form.debtRating" type="textarea" :placeholder="$t('违约前债项评级')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('最新债项评级')" prop="latestDebtRating">
            <el-input v-model="form.latestDebtRating" type="textarea" :placeholder="$t('最新债项评级')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('债券担保情况说明')" prop="situationDesc">
            <el-input v-model="form.situationDesc" type="textarea" :placeholder="$t('债券担保情况说明')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('是否为城投债')" prop="cityInvestBond">
            <dict-select
              v-model="form.cityInvestBond"
              dictionary="city_invest_bond"
              :placeholder="$t('是否为城投债')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('债券发行主体名称')" prop="issuedSubjectName">
            <el-input v-model="form.issuedSubjectName" :placeholder="$t('债券发行主体名称')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('债券发行主体统一社会信用代码')" prop="socialCreditCode">
            <el-input v-model="form.socialCreditCode" :placeholder="$t('债券发行主体统一社会信用代码')" show-word-limit maxlength="18" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('发债企业类型1')" prop="enterpriseTypeFirst">
            <dict-select
              v-model="form.enterpriseTypeFirst"
              dictionary="enterprise_type_first"
              :placeholder="$t('发债企业类型1')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('发债企业类型2')" prop="enterpriseTypeSecond">
            <dict-select
              v-model="form.enterpriseTypeSecond"
              dictionary="enterprise_type_second "
              :placeholder="$t('发债企业类型2')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('发债企业所属地区')" prop="area">
            <el-cascader
              v-model="form.area"
              :options="areaOptions"
              :props="{ value: 'id' , label: 'name'}"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('发债企业所属行业')" prop="industry">
            <dict-select
              v-model="form.industry"
              dictionary="industry"
              :placeholder="$t('发债企业所属行业')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('发生风险前主体评级')" prop="subjectRating">
            <el-input v-model="form.subjectRating" type="textarea" :placeholder="$t('发生风险前主体评级')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" :placeholder="$t('备注')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div v-if="!btnHide && processBtn" slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="fund:monitor" />
    </div>
  </div>
</template>

<script>
    import { saveFundDefaultBond, getFundDefaultBond } from '@/api/fund/operation/fundDefaultBond'
    import { treeSelect } from '@/api/system/area/sysArea'
    export default {
        data() {
            return {
                // 表单参数
                form: {},
                // 遮罩层
                loading: false,
                areaOptions: [],
                // 表单校验
                rules: {
                name: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                socialCreditCode: { message: this.$t('只能输入数字或字母'), pattern: /^[a-z0-9]+$/i, trigger: 'blur' }
                },
              processBtn: false,
              btnHide: false
            }
        },
        created() {
          treeSelect({ parentId: '1' }).then(res => {
            res.data.forEach(item => {
              item.name = this.$t(item.name)
              item.children.forEach(item2 => {
                item2.name = this.$t(item2.name)
              })
              this.areaOptions = res.data
            })
          })
        },
        methods: {
            // 初始化数据
            init(data) {
                this.processBtn = data.processBtn
                this.btnHide = data.btnHide
                this.loading = true
                if (data.id !== undefined) {
                    getFundDefaultBond(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    })
                } else {
                    this.reset()
                    this.form.monitorId = data.monitorId
                    this.form.num = data.total + 1
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,

                    monitorId: undefined,

                    name: undefined,

                    number: undefined,

                    marketValue: undefined,

                    valuationBasis: undefined,

                    cost: undefined,

                    negative: undefined,

                    issuedDate: undefined,

                    expireDate: undefined,

                    defaultDate: undefined,

                    faceValue: undefined,

                    principal: undefined,

                    interest: undefined,

                    type: undefined,

                    debtRating: undefined,

                    latestDebtRating: undefined,

                    situationDesc: undefined,

                    cityInvestBond: undefined,

                    issuedSubjectName: undefined,

                    socialCreditCode: undefined,

                    enterpriseTypeFirst: undefined,

                    enterpriseTypeSecond: undefined,

                    area: undefined,

                    industry: undefined,

                    subjectRating: undefined,

                    remark: undefined

                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveFundDefaultBond(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                })
           }

        }

    }
</script>
